package com.example.seakdreamandroid;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.seakdreamandroid.entity.AudioInfo;
import com.example.seakdreamandroid.entity.Favorite;

import java.util.ArrayList;
import java.util.List;

import cn.smssdk.gui.DefaultContactViewItem;

public class FavoriteAdapter extends BaseAdapter {

    private Context mContext;
    private List<Favorite> data;
    private int layoutResId;

    public FavoriteAdapter(Context mContext, List<Favorite> data, int layoutResId){
        this.mContext = mContext;
        this.data = data;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        if (data != null){
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (data != null){
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            //加载item的布局文件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId, null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.title = convertView.findViewById(R.id.title);
            viewHolder.audioNo = convertView.findViewById(R.id.audio_no);
            viewHolder.audioAuthor = convertView.findViewById(R.id.audio_author);

            //将ViewHolder对象同convertView关联起来
            convertView.setTag(viewHolder);
        }else {
            //重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        if (data.get(position).getIsWhite() == 1){
            viewHolder.title.setText("白噪音");
            if (data.get(position).getWhiteId3() != null){
                viewHolder.audioAuthor.setText(data.get(position).getWhiteName1() + "  " + data.get(position).getWhiteName2() + "  " + data.get(position).getWhiteName3());
            }else if (data.get(position).getWhiteId2() != null){
                viewHolder.audioAuthor.setText(data.get(position).getWhiteName1() + "  " + data.get(position).getWhiteName2());
            }else{
                viewHolder.audioAuthor.setText(data.get(position).getWhiteName1());
            }
        }else{
            viewHolder.title.setText(data.get(position).getAudioName());
            viewHolder.audioAuthor.setText(data.get(position).getAudioAuthor());
        }
        viewHolder.audioNo.setText(position + 1 + "");
        return convertView;
    }

    private static class ViewHolder{
        //每一个item中的控件
        TextView title;
        TextView audioNo;
        TextView audioAuthor;

    }
}
